#include<iostream>
#include<vector>
#include<queue>
#include<memory>
#include<string>
#include"Cond.hpp"
#include"Log.hpp"
#include"Task.hpp"
#include"Mutex.hpp"
#include"Thread.hpp"

namespace ThPoolModule
{
    
    using namespace CondModule;
    using namespace LogModule;
    using namespace Lock;
    using namespace Thread;


    void DefaultTest()
    {
        while (true)
        {
            LOG(LogLevel::DEBUG) << "我是一个测试方法";
            sleep(1);
        }
    }


    using thread_t =std::shared_ptr<myThread>;
    const int defaultnum = 5;
    template<class T>
    class ThreadPool
    {
        public:
        bool isEmpty()
        {
            return _task.empty();
        }
        void HandlerTask(std::string name)
        {
            LOG(LogLevel::INFO) << "线程: " << name << ", 进入HandlerTask的逻辑";
            while(true)
            {
                T t;
                {
                    LockGuard guard(_lock);
                    while(isEmpty() && _isrunning)
                    {
                        _wait_num++;
                        _cond.Wait(_lock);
                        _wait_num--;
                    }

                    //如果任务为空 并且 是进程不在进行
                    if(isEmpty() && !_isrunning)
                    {
                        break;
                    }
                    
                    t = _task.front();
                    _task.pop();
                }

                t(name);//任务必须提供()方法 符号重定义

            }
             LOG(LogLevel::INFO) << "线程: " << name << " 退出";
        }

        ThreadPool(int num = defaultnum)
        :_num(num)
        ,_wait_num(0)
        ,_isrunning(false)
        {
            for(int i =0;i<_num;i++)
            {
                //绑定函数 绑定对象  占位符
                _threads.push_back(std::make_shared<myThread>(std::bind(&ThreadPool::HandlerTask,this,std::placeholders::_1)));
                LOG(LogLevel::INFO) << "构建线程" << _threads.back()->Name() << "对象 ... 成功";
            }
        }
        void Equeue(const T&& in)//生产队列
        {
            LockGuard lockguard(_lock);
            if(!_isrunning) return;
            _task.push(std::move(in));

            if(_wait_num>0)
            _cond.Signal();
        }
        void Start()
        {
            if(_isrunning)
            return;
            _isrunning = true; 
            for (auto &thread_ptr : _threads)
            {
                LOG(LogLevel::INFO) << "启动线程" << thread_ptr->Name() << " ... 成功";
                thread_ptr->Start();
            }

        }
        void Wait()
        {
            for(auto &thread_ptr : _threads)
            {
                LOG(LogLevel::INFO) << "线程阻塞" << thread_ptr->Name() << " ... 成功";
                thread_ptr->Join();
            }
        }
        void Stop()
        {
            //不再入任务了
            LockGuard lockguard(_lock);

            if(_isrunning)
            {
                _isrunning = false; //不用再执行任务了
                if(_wait_num>0)
                    _cond.SingalAll();
            }
        }

        ~ThreadPool()
        {
        }


        private:
        std::vector<thread_t> _threads;
        int _num;  //进程总共数目
        int _wait_num; //阻塞进程的数目
        std::queue<T> _task; //任务队列
        
        Mutex _lock;
        Cond _cond;

        bool _isrunning;
    };

}